package com.cx.mall.pay.controller;

import com.cx.mall.common.util.ResponseVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.function.StreamBridge;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/test")
public class TestController {

    @Autowired
    private StreamBridge streamBridge;

    /**
     * 测试发送支付通知消息
     */
    @GetMapping("/sendPayNotify")
    public ResponseVo sendPayNotify(@RequestParam String orderId) {
        Message<?> message = MessageBuilder
                .withPayload(orderId) //消息体
                .setHeader("tradeNo_Id", "TEST_" + orderId) //消息头
                .build();
        
        log.info("发送支付通知消息: {}", message);
        streamBridge.send("payNotifyOutPut-out-0", message);
        return ResponseVo.success("支付通知消息发送成功");
    }

    /**
     * 测试支付服务健康状态
     */
    @GetMapping("/health")
    public ResponseVo health() {
        log.info("支付服务健康检查");
        return ResponseVo.success("支付服务运行正常");
    }

    /**
     * 测试消息发送状态
     */
    @GetMapping("/producer-status")
    public ResponseVo producerStatus() {
        log.info("检查消息生产者状态");
        return ResponseVo.success("消息生产者已配置，可以发送消息...");
    }
}
